package day05

import "strconv"

// day05
//
// 738. 单调递增的数字
//
// https://leetcode.cn/problems/monotone-increasing-digits/
//
// ```go
func monotoneIncreasingDigits(n int) int {
	s := strconv.Itoa(n)
	str := []byte(s)
	x := len(str)
	if x <= 1 {
		return n
	}
	for i := x - 1; i > 0; i-- {
		if str[i-1] > str[i] {
			str[i-1]--
			for j := i; j < x; j++ {
				str[j] = '9'
			}
		}
	}
	ans, _ := strconv.Atoi(string(str))
	return ans
}

//```

// 968. 监控二叉树
//
//https://leetcode.cn/problems/binary-tree-cameras/
//
//```go
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func minCameraCover(root *TreeNode) int {
	ans := 0
	var dfs func(root *TreeNode) int
	dfs = func(root *TreeNode) int {
		if root == nil {
			return 2
		}
		l := dfs(root.Left)
		r := dfs(root.Right)
		if l == 2 && r == 2 {
			return 0
		}
		if l == 0 || r == 0 {
			ans++
			return 1
		}
		if l == 1 || r == 1 {
			return 2
		}
		return -1
	}
	if dfs(root) == 0 {
		ans++
	}
	return ans
}

//```
